<script language="javascript" type="text/javascript" src="__PUBLIC__/plugin/autocomplete/jquery.autocomplete.js"></script>
<link rel="stylesheet" href="__PUBLIC__/plugin/autocomplete/jquery.autocomplete.css">
<script type="text/javascript" src="/Public/js/jquery.form.min.js"></script>
<div class="form">
    <form method='post' id="form_do" name="form_do" action="{:U(MODULE_NAME. '/Product/add')}">
      <dl>
        <dt> 所属类型：</dt>
        <dd>
          <foreach name="cate_name" item="cate_name" key="key">
            <input type="checkbox" name="catid[]" value="{$cate_name.id}"/>{$cate_name.name}
          </foreach>
        </dd>
      </dl>
    <dl>
        <dt>线路名称：</dt>
        <dd>
          <input type="text" name="title" class="inp_one inp_w550" value="{$vo.title}" /><font color="#FF0000">*</font>
          &nbsp;<input type="checkbox" name="verify" value="1"
          <if condition='$vo.verify eq 1'>checked</if>
          />&nbsp;二次确认 </dd>
      </dl>
      <dl>
        <dt>扩展名称：</dt>
        <dd>
          <input type="text" name="extend" class="inp_one inp_w550" value="{$vo.extend}" />
        </dd>
      </dl>
      <dl>
         <dt>供应商：</dt>
         <dd>
           <input type="text" name="supplier" id="supplier">
         </dd>
      </dl>

      <dl>
        <dt> 出发地：</dt>
        <dd>
          <span id="departure_add">
            <div>
              <select name="province[]" id="chinaprovince">
                {$departure_province}
              </select>
              <span onclick="departure_del(this)" class="span_del">
                <a href="javascript:;" class="button-red" style="padding: 2px 10px;">删除</a>
              </span>
              
            </div>
          </span>
          <div><input type="checkbox" id="check"/>当地游</div>
          <div><input type="checkbox" name="visa" value="" id="visa" onchange="visaValue(this)" />签证类</div>
        </dd>
      </dl>
      <dl style="display:none;" id="local">
        <dt></dt>
        <dd>
          <foreach name="localplay" item="localplay" key="key">
            <span style="margin-right:10px;" ><input type="checkbox" name="localid[]" value="{$localplay.id}"/>{$localplay.name}</span>
          </foreach>
        </dd>
      </dl>
      <dl>
        <dt></dt>
        <dd><div><span class="button-green" onclick="departure_add()">添加多出发地</span></div></dd>
      </dl>
      <dl>
        <dt> 目的地：</dt>
        <dd>
          <div id="destination_add">
            <div>
            <select name="continent[]" onchange="destinationChange(this,1)">
                {$destination}
            </select>
            <select name="country[]"  onchange="destinationChange(this,2)">
              <option>请选择国家</option>
            </select>
            <select name="city[]" onchange="destinationChange(this,3)">
              <option>请选择城市</option>
            </select>
            <span onclick="destination_del(this)" class="span_del"><a href="javascript:;" class="button-red" style="padding: 2px 10px;">删除</a></span>
            </div>
          </div>
          <div><span onclick="destination_add()" class="button-green">添加多目的地</span></div>
        </dd>
      </dl>
      <dl style="display:none;" id="destination_add">
        <dt></dt>
        <dd>
        </dd>
      </dl>
      <dl id="ship" style="display:none;">
        <dt> 邮轮：</dt>
        <dd>
          <select name="ship" id="ship_select">
            {$select_ship}
          </select>
        </dd>
      </dl>
      <input type="hidden" class="inp_default" name="departure_time" id="departure_time" value="{:date('Y-m-d')}">
        <dl>
        <dt> 行程天数 ：</dt>
        <dd><input type="text" name="travel_days" class="inp_one inp_w30" value="{$vo.travel_days}" /> 日 <input type="text" name="travel_night" class="inp_one inp_w30"  value="{$vo.travel_night}"/> 晚
        </dd>
      </dl>
      <dl>
        <dt> 市场价：</dt>
        <dd>
          <input type="text" name="market_price" class="inp_small" /> /人起
        </dd>
      </dl>
      <dl>
        <dt> 现价：</dt>
        <dd>
          <input type="text" name="price" class="inp_small" /> /人起
        </dd>
      </dl>
       <dl>

      	<dt> 产品缩略图：</dt>

		<dd>

			<div class="litpic_show">

			    <div style="float:left;">

			    <input type="text" class="inp_w250" name="litpic" readonly id="litpic"  value="{$vo.litpic}" />

			    </div>
			
				<div class="litpic_btn" id="litpic_btn">

			        <span>添加缩略图</span>

			        <input id="fileupload" type="file" name="mypic">

			    </div>

			    <font color="red">(尺寸:320px*210px)</font>

			    <div class="litpic_tip"></div>

			    <div id="litpic_show">

			    <if condition="$vo['litpic']"><img src="{$vo.litpic}" width='120' /></if>

			    </div>

			</div>

		</dd>

	</dl>

           		

    <dl>

		<dt> 产品大图：</dt>

		<dd>

		<div class="bigpic_show">

			<div style="float:left;">

		    <input type="text" class="inp_w250" readonly name="bigpic" id="bigpic"  value="{$vo.bigpic}" />

		    </div>

				<div class="litpic_btn" id="bigpic_btn" >

		        <span>添加大图</span>

		        <input id="fileupload2" type="file" name="mypic2">

		    </div>

		    <font color="red">(尺寸:宽度1920px为佳，比例2:1)</font>

		    <div class="bigpic_tip"></div>

		    <div id="bigpic_show">

		    <if condition="$vo['bigpic']"><img src="{$vo.bigpic}" width='120' /></if>

		    </div>

		</div>

		</dd>

	</dl>
   <input type="hidden" class="inp_one" name="enddate" id="enddate" value="{:date('Y-m-d H:i:s',time())}">
      <dl>
        <dt> 关键词：</dt>
        <dd>
          <input type="text" name="keywords" class="inp_w250" value="" /><span class="tip">多关键词之间以英文逗号","分隔</span></dd>
      </dl>
      <div class="form_b" style="padding-left:110px;">
        <input type="hidden" name="id" value="{$vo.id}" />
        <input type="hidden" name="pid" value="{$pid}" />
        <input type="submit" class="button-green" id="submit" value="提 交">
      </div>
    </form>
  <input type="hidden" id="departure_province" value="{$departure_province}" />
  <input type="hidden" id="destination_continent" value="{$destination_continent}" />
</div>
<script type="text/javascript">
 /**
 * [visaValue description]
 * @param  {[type]} obj [description]
 * @return {[type]}     [description]
 */
function visaValue(obj)
{
  var checked = $(obj).attr('checked');

  if(checked == 'checked')
    {
      $(obj).val('1');
    }
    else
    {
      $(obj).val('');
    }
}
//autocomplete
function formatItem(row){return row;}
function findValue(li) { 
	if( li == null ) return false; 
	var sValue = li.selectValue; 
	if( !!li.extra )
	{
		sValue = li.extra[0];
	}
}
function selectItem(li) {findValue(li);} 

$(function(){
  $("#fileupload").wrap("<form id='myupload' action='{:U(MODULE_NAME. '/Public/uploadPic',array('fileType' =>'Img','type'=>'Product'))}' method='post' enctype='multipart/form-data'></form>");
  $("#fileupload").change(function(){
	  	var tip = $(".litpic_tip");
	  	var btn = $("#litpic_btn span");
	  	
		if($("#fileupload").val() == "") return;
		
		$("#myupload").ajaxSubmit({
			dataType:  'json',
			beforeSend: function() {
				$('#litpic_show').empty();
				btn.html("上传中...");
			},
      		success: function(data) {
	        	if(data.state == 'SUCCESS')
	        	{
	        		tip.html(""+ data.title +" 上传成功("+data.size+"k)");
					var img = data.url;
					var timg = data.turl;
					$('#litpic_show').html("<img src='{$website_url}"+timg+"' width='120'>");
					$("#litpic").val(timg);
		        }
	        	else 
	        	{
	        		tip.html(data.state);
		        }
		        btn.html("更换图片");
     	 	},
			error:function(xhr){
				btn.html("上传失败");
				tip.html(xhr);
			}
    });	
  });

  $("#fileupload2").wrap("<form id='myupload2' action='{:U(MODULE_NAME. '/Public/uploadPic',array('fileType' =>'Img','type'=>'Product'))}' method='post' enctype='multipart/form-data'></form>");
  $("#fileupload2").change(function(){
		var tip = $(".bigpic_tip");
		var btn = $("#bigpic_btn span");
   	  
		if($("#fileupload2").val() == "") return;
      
		$("#myupload2").ajaxSubmit({
			dataType:  'json',
			beforeSend: function() {
			      $('#bigpic_show').empty();
			      btn.html("上传中...");
			},
			success: function(data) {
			  if(data.state == 'SUCCESS')
			  {
					tip.html(""+ data.title +" 上传成功("+data.size+"k)");
				  	var img = data.url;//原图
					var timg = data.turl;//缩略图
					$('#bigpic_show').html("<img src='{$website_url}"+timg+"' width='120'>");
					$("#bigpic").val(timg);
			  }
			  else 
			  {
				  	tip.html(data.state);
			  }
			  btn.html("更换图片");
			},
		   error:function(xhr){
				btn.html("上传失败");
				tip.html(xhr);
		   }
    });
  });

    $('#check').change(function(){
        var a = $('#check').attr('checked');
        if(a == 'checked'){
          $('#chinaprovince').val('');
          $('#chinaprovince').attr('disabled',true);
          $('#local').removeAttr('style');
        }else{
          $('#chinaprovince').removeAttr('disabled');
          $('#local').css('display','none');
        }
    });
    
	$('#supplier').autocomplete("{:U('Home/Public/autoSupplierComplete')}",
	{
		delay:10, 
		minChars:1, 
		matchSubset:1, 
		matchContains:1, 
		cacheLength:10, 
		onItemSelect:selectItem, 
		onFindValue:findValue, 
		formatItem:formatItem, 
		autoFill:false 
	});
});

function departure_add()
{
  var select = '<div><select name="province[]">';
  select += $("#departure_province").val();
  select += '</select>';
  select += '<span onclick="departure_del(this)" class="span_del"> <a href="javascript:;" class="button-red" style="padding: 2px 10px;">删除</a></span></div>';
  $('#departure_add').append(select);
}

function departure_del(o)
{
  i = $(o).parent().parent().find("div").length;
  if(i==1)
  {
    alert("最少保留一个选择项");
  }
  else
  {
    $(o).parent().remove();
  }
}

function destination_add()
{

    var select = '';
  	select += '<div><select name="continent[]" onchange="destination(this,1)">';
    select += $("#destination_continent").val();
    select += '</select>';
    select += '<select name="country[]"  onchange="destination(this,2)">';
    select += '<option value="">请选择国家</option>';
    select += '</select>';
    select += '<select name="city[]">';
    select += '<option value="">请选择城市</option>';
    select += '</select>';
    select += '<span onclick="destination_del(this)" class="span_del"><a href="javascript:;" class="button-red" style="padding: 2px 10px;">删除</a></span></div>';
    $('#destination_add').append(select);
}

function destination_del(o)
{
  i = $(o).parent().parent().find("div").length;
  if(i==1)
  {
    alert("最少保留一个选择项");
  }
  else
  {
    $(o).parent().remove();
  }
}

function destinationChange(o,type)
{
  var div = $(o).parent();
  var get = {};
  var obj = $(o);
  
  if(type==1)
  {
  get.continent = div.find('[name="continent[]"]').val();
    obj = div.find('[name="country[]"]');
  }
  else if(type==2)
  {
    get.country = div.find('[name="country[]"]').val();
    obj = div.find('[name="city[]"]');
  }
  else
  {
    return false;
  }

  $.get('/Home/Public/ajaxDestination.html',get, function (data, textStatus){
    obj.html(data);
  });
}
</script>